package com.alipay.generalize;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayOpenPublicQrcodeCreateModel;
import com.alipay.api.domain.AlipayOpenPublicShortlinkCreateModel;
import com.alipay.api.domain.CodeInfo;
import com.alipay.api.domain.Scene;
import com.alipay.api.request.AlipayOpenPublicQrcodeCreateRequest;
import com.alipay.api.request.AlipayOpenPublicShortlinkCreateRequest;
import com.alipay.api.response.AlipayOpenPublicQrcodeCreateResponse;
import com.alipay.api.response.AlipayOpenPublicShortlinkCreateResponse;
import com.alipay.factory.AlipayAPIClientFactory;
import com.alipay.util.RequestUtil;

import net.sf.json.JSONObject;

/**
 * 推广支持接口调用示例  (测试通过)
 * 
 * @author liliang
 *
 */
public class AlipayOpenGeneralize extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	static String QRCODE_CREATE_RESPONSE_OBJECT = "alipay_open_public_qrcode_create_response";// 推广二维码响应参数
	static String SHORTLINK_CREATE_RESPONSE_OBJECT = "alipay_open_public_shortlink_create_response";// 推广短链接响应参数
	static String QRCODE_TYPE = "1";// 二维码
	static String SHORTLINK_TYPE = "2";// 短链接

	/**
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	/**
	 * 网关处理
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		Map<String, String> params = RequestUtil.getRequestParams(req);
/*
		String queryString = "";
		for (String key : params.keySet()) {
			String[] values = params.get(key);
			for (int i = 0; i < values.length; i++) {
				String value = values[i];
				queryString += key + "=" + value + "&";
			}
		} // 去掉最后一个空格
		queryString = queryString.substring(0, queryString.length() - 1);
		System.out.println(queryString);
		System.out.println("GET " + req.getRequestURL() + " " + queryString);*/

		String type = params.get("type");
		if (QRCODE_TYPE.equals(type)) {// 二维码
			System.out.println("生成二维码图片...");
			qrcodeCreate(resp);
		} else if (SHORTLINK_TYPE.equals(type)) {// 短连接
			System.out.println("生成短连接...");
			shortlinkCreate(resp);
		}
	}
	// 支付宝响应消息

	// 带参推广二维码接口
	public void qrcodeCreate(HttpServletResponse resp) {
		AlipayClient alipayClient = AlipayAPIClientFactory.getAlipayClient();
		AlipayOpenPublicQrcodeCreateRequest request = new AlipayOpenPublicQrcodeCreateRequest();
		AlipayOpenPublicQrcodeCreateModel model = new AlipayOpenPublicQrcodeCreateModel();
		CodeInfo codeInfo = new CodeInfo();
		Scene scene = new Scene();
		scene.setSceneId("1");
		codeInfo.setGotoUrl(
				"http://baike.baidu.com/link?url=l4RBNJwNWp3w_FFEsjfklpDVD_SgK7Km5G4eE7qCwQazzzeT8zLa76TQJWw4EEzLiB1jQcfoHK_7hMtkoyBkUHVDJEPoORJQxoUetWfqRozsltxuQiEemNPkun7fqING");
		codeInfo.setScene(scene);
		model.setCodeInfo(codeInfo);
		model.setCodeType("TEMP");
		model.setExpireSecond("1800");
		model.setShowLogo("Y");
		request.setBizModel(model);
		System.out.println(request.getBizContent());
		AlipayOpenPublicQrcodeCreateResponse response = new AlipayOpenPublicQrcodeCreateResponse();
		try {
			response = alipayClient.execute(request);
			/*
			 * code_img 二维码图片地址，可跳转到实际图片 expire_second 二维码有效时间，单位（秒）。永久码暂时忽略该信息
			 */
			String responseBody = response.getBody();
			JSONObject resultJson = JSONObject.fromObject(responseBody);
			JSONObject o = (JSONObject) resultJson.get(QRCODE_CREATE_RESPONSE_OBJECT);
			System.out.println("二维码图片====>" + o.getString("code_img"));
			System.out.println("二维码有效时间====>" + o.getString("expire_second"));

			resp.sendRedirect(o.getString("code_img"));
		} catch (Exception e) {
			// TODO: 异常处理
		}
	}

	// 带参推广短链接接口
	public void shortlinkCreate(HttpServletResponse resp) {
		AlipayClient alipayClient = AlipayAPIClientFactory.getAlipayClient();
		AlipayOpenPublicShortlinkCreateRequest request = new AlipayOpenPublicShortlinkCreateRequest();
		AlipayOpenPublicShortlinkCreateModel model = new AlipayOpenPublicShortlinkCreateModel();
		model.setSceneId("2");
		model.setRemark("门店1支付推广");
		request.setBizModel(model);
		AlipayOpenPublicShortlinkCreateResponse response = new AlipayOpenPublicShortlinkCreateResponse();
		try {
			response = alipayClient.execute(request);
			/* shortlink 生成的带参推广短链接 */
			String responseBody = response.getBody();
			JSONObject resultJson = JSONObject.fromObject(responseBody);
			JSONObject o = (JSONObject) resultJson.get(SHORTLINK_CREATE_RESPONSE_OBJECT);
			System.out.println("推广短链接=====>" + o.getString("shortlink"));
			resp.sendRedirect(o.getString("shortlink"));
		} catch (Exception e) {
			// TODO: 异常处理
		}
	}
	/*
	 * public static void main(String[] args) { qrcodeCreate(); //
	 * shortlinkCreate(); }
	 */
}
